博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重写Repeater控件,并分页管理
阅读量:6435 次
发布时间:2019-06-23

本文共 9941 字,大约阅读时间需要 33 分钟。

1、重写Repeater(修改过的,当只有一页的情况时,只显示Total页数及共有多少条目,不会显示上一页,下一页等):

View Code
using System.ComponentModel;using System.Web.UI;[assembly: TagPrefix("MyRepeater.Control", "MyRepeater")]namespace MyRepeater{    ///     /// JRepeater控件    ///     [DefaultProperty("Text"), ToolboxData("
<{0}:Repeater ID=\"Rep_List\" runat=server EnableViewState=\"false\" OnPreRender=\"Rep_List_PreRender\">
")] public class Repeater : System.Web.UI.WebControls.Repeater { public bool ShowPagingWhenItemLessThanPageSize { get; set; } private int _recordcount = 0; private int _pagesize = 0; private string _pagelink = ""; private int _currentpage = 1; private string _urlSearchPrex = null; /// /// constructure /// public Repeater() { this.ShowPagingWhenItemLessThanPageSize = true; } #region _ [Bindable(true), Category("Data"), DefaultValue("1"), Description("totalCount")] public int RecordCount { get { return _recordcount; } set { _recordcount = value; } } [Bindable(true), Category("Data"), DefaultValue("1"), Description("pagesize")] public int PageSize { get { return _pagesize; } set { _pagesize = value; } } [Bindable(true), Category("Data"), DefaultValue(""), Description("currentLink")] public string PageLink { get { return _pagelink; } set { _pagelink = value; } } [Bindable(true), Category("Data"), DefaultValue("1"), Description("currentPageIndex")] public int CurrentPage { get { return _currentpage; } set { _currentpage = value; } } public string UrlSearchPrex { get { return _urlSearchPrex; } set { _urlSearchPrex = value; } } #endregion /// /// output control to page /// /// protected override void Render(HtmlTextWriter output) { base.Render(output); if (this._recordcount <= this._pagesize) { output.WriteLine("
Total:1 page(s)   " + this._recordcount + " record(s)
"); return; } if (!(this._recordcount <= this._pagesize && !this.ShowPagingWhenItemLessThanPageSize)) { output.WriteLine("
" + Pagination(_recordcount, _pagesize, _currentpage, _pagelink, _urlSearchPrex) + "
"); } } /// /// paging function /// /// total count /// /// /// ///
public string Pagination(int recordcount, int pagesize, int currentpage, string url, string urlSearchPrex) { int allcurrentpage = 0; int next = 0; int pre = 0; int startcount = 0; int endcount = 0; string currentpagestr = ""; if (currentpage < 1) { currentpage = 1; } //total page count if (pagesize != 0) { allcurrentpage = (recordcount / pagesize); allcurrentpage = ((recordcount % pagesize) != 0 ? allcurrentpage + 1 : allcurrentpage); allcurrentpage = (allcurrentpage == 0 ? 1 : allcurrentpage); } next = currentpage + 1; pre = currentpage - 1; startcount = (currentpage + 5) > allcurrentpage ? allcurrentpage - 9 : currentpage - 4; endcount = currentpage < 5 ? 10 : currentpage + 5; if (startcount < 1) { startcount = 1; } if (allcurrentpage < endcount) { endcount = allcurrentpage; } currentpagestr = "Total: " + allcurrentpage + "  page(s)    " + recordcount + " record(s)      "; string checkaccountName = Page.Request.QueryString["checkaccount"]; string checkprojectName = Page.Request.QueryString["checkproject"]; urlSearchPrex += "&checkaccount=" + checkaccountName + "&checkproject=" + checkprojectName; string urlPrex = url + urlSearchPrex == null ? "?page=" : "?" + urlSearchPrex + "&page="; currentpagestr += currentpage > 1 ? " First      < Prev " : "  First "; for (int i = startcount; i <= endcount; i++) { currentpagestr += currentpage == i ? " " + "[ " + i + " ]" + "" : " " + "[ " + i + " ]" + " "; } currentpagestr += currentpage != allcurrentpage ? "   Next >     Last " : "    Last "; //currentpagestr += "   "; //currentpagestr += "   GO"; return currentpagestr; } }}

2、页面调用方法:

View Code
Emp No. Email ID Role
" name="ckboxChild" style="display: inline" /> <%# Eval("EmpNo") %> <%# Eval("EmailID") %> <%# Eval("Role") %>

后台只需要把数据绑定;

3、常用高效数据库分页存储过程(借用的):

View Code
--高效分页存储过程      CREATE PROCEDURE [dbo].[GetListByPage](   @Table nvarchar(500),          --表名   @Field nvarchar(500) = '*',        --读取字段   @Where  nvarchar(max) = NULL,       --Where条件   @GroupBy nvarchar(500) = NULL,      --分组   @OrderBy nvarchar(500)= NULL,       --排序字段   @PrimaryKeyField nvarchar(50),      --主键必需    @PageIndex int = 1,            --开始页码   @PageSize int = 10,             --页大小   @IsCount bit = 0          --返回记录总数     ------------------------------------------------------------------------------------------------   --当@IsCount为1时,将同时返回2张表,表0为记录总数,表1为查询结果  ------------------------------------------------------------------------------------------------     )   AS   BEGIN       ------------------------------------------------------------------------------------------------       DECLARE @strWhere nvarchar(500)                     --Where 条件       IF @Where IS NOT NULL AND @Where != ''              --Where 条件       BEGIN           SET @strWhere = ' WHERE ' + @Where + ' '       END       ELSE       BEGIN           SET @strWhere = ''       END       ----------------------------------------------------------------------------------------------------       DECLARE @strGroupBy nvarchar(500)                   --GroupBy 条件       IF @GroupBy IS NOT NULL AND @GroupBy != ''          --GroupBy 条件       BEGIN           SET @strGroupBy = ' GROUP BY ' + @GroupBy + ' '       END       ELSE       BEGIN           SET @strGroupBy = ''       END       ----------------------------------------------------------------------------------------------------       DECLARE @strOrderBy nvarchar(500)                   --OrderBy 条件       IF @OrderBy IS NULL OR @OrderBy = ''                --OrderBy 条件       BEGIN           SET @strOrderBy = ' ORDER BY ' + @PrimaryKeyField + ' DESC'       END       ELSE       BEGIN           SET @strOrderBy = ' ORDER BY ' + @OrderBy       END       ----------------------------------------------------------------------------------------------------       DECLARE @strSql nvarchar(max)   --Sql 语句       --计算总行数       IF @IsCount = 1       BEGIN           SET @strSql= 'SELECT  Count (*) AS RecordCount FROM ' + @Table + @strWhere + @strGroupBy           print @strSql          EXEC sp_executesql @strSql           --RETURN        END                  ----------------------------------------------------------------------------------------------------       IF @PageIndex < 1                                  --第一页提高性能       BEGIN              SET @PageIndex = 1       END         IF @PageIndex = 1                                 BEGIN           SET @strSql = 'SELECT TOP ' + str(@PageSize) +  ' ' + @Field + ' FROM ' + @Table +                           @strWhere + @strGroupBy + @strOrderBy           print @strSql          EXEC sp_executesql @strSql           RETURN       END       ----------------------------------------------------------------------------------------------------         DECLARE @STARTID nvarchar(50)       DECLARE @ENDID nvarchar(50)       SET @STARTID = convert(nvarchar(50),(@PageIndex - 1) * @PageSize + 1)       SET @ENDID = convert(nvarchar(50),@PageIndex * @PageSize)       SET @strSql = 'WITH MYTABLE AS (SELECT ROW_NUMBER() OVER (' + @strOrderBy + ')                      AS RowNumber,' + @Field + ' FROM '+ @Table +  @strWhere + @strGroupBy + ')                       SELECT * FROM MYTABLE                       WHERE RowNumber BETWEEN ' + @STARTID + ' AND ' + @ENDID       print @strSql      EXEC sp_executesql @strSql       --------------------------------------------------------------------------------------------------   END

4、有上面的存储过程的使用,同样可以在后台实现真分页

 

转载于:https://www.cnblogs.com/52life/archive/2013/03/08/2949286.html

你可能感兴趣的文章
Android:简易单词本(三)
查看>>
使用SCVMM2012从hyper-v 2.0平台往hyper-v 3.0平台迁移VM虚拟机的报错(2)
查看>>
RocketMQ的安装与配置
查看>>
Android studio 单元测试
查看>>
LINUX--特殊权限SUID,SGID,Sticky
查看>>
Java基础学习总结(1)——equals方法
查看>>
使用 MYSQLBINLOG 来恢复数据
查看>>
DB2更改数据文件路径
查看>>
嵌入式Linux裸机开发(六)——S5PV210时钟系统
查看>>
大型网站技术架构(二)架构模式
查看>>
EFCore笔记之异步查询
查看>>
EntityFramework 启用迁移 Enable-Migrations 报异常 "No context type was found in the assembly"
查看>>
RabbitMQ学习总结(2)——安装、配置与监控
查看>>
Distributed Configuration Management Platform(分布式配置管理平台)
查看>>
lsof 详解 (转载)
查看>>
apache poi 解析excel
查看>>
Myeclipse优化配置
查看>>
Mysql学习总结(5)——MySql常用函数大全讲解
查看>>
Exchange Server 2007迁移Exchange Server 2010 (16)--- OWA重定向
查看>>
Effective C++:unio
查看>>